<?php
namespace AuctionCore\CarResource;

/**
 * 车商查询维保记录model
 * Class DealerRepairLog
 * @package AuctionCore\CarResource
 */
class DealerRepairLog {

	protected $dealer_id = null;

	/**
	 * 构造
	 * @param  int $car_id  车辆id
	 * @return array
	 */
	public function __construct($dealer_id=0) {
		$this->dealer_id = $dealer_id;
	}

	/**
	 * @param $page
	 * @param $listRows
	 */
	public function get_logs($page,$listRows){
		$prefix = C('DB_PREFIX');
		$sql = "select r.id as id,r.dealer_id as dealer_id,r.rid as rid,r.manufacturer as manufacturer,r.price as price,
				date_format(r.createtime,'%Y-%m-%d %H:%i') as createtime,r.vin as vin,r.search_state as search_state,r.pay_state as pay_state,r.type as type,
				c.car_id as car_id,c.mbrand_id as mbrand_id,c.series_id as series_id,c.brand_id as brand_id from {$prefix}dealer_repair_log as r LEFT JOIN
				{$prefix}cars as c ON c.vin=r.vin WHERE
				r.dealer_id=".$this->dealer_id." ORDER BY r.createtime DESC LIMIT ".$listRows*($page-1)." ,".$listRows;
		return M()->query($sql);
	}

	/**通过车辆编号
	 * @param $car_id
	 * @return mixed
	 */
	public function get_order_by_carid($car_id){
		return M('order')->field('order_id,order_no')->where('car_id='.$car_id.' and status in(3,301,4)')->find();
	}

	/**count
	 * @return mixed
	 */
	public function get_count(){
		return M('dealer_repair_log')->field('count(1) as total')->where('dealer_id='.$this->dealer_id)->find();
	}

	/**增加新日志
	 * @param $data
	 * @return bool
	 */
	public function save_log($data){
		$is_had = M('dealer_statistics')->where("dealer_id =".$this->dealer_id." and date='".date('Y-m-d')."'")->find();
		if($is_had){
			M('dealer_statistics')->where("dealer_id =".$this->dealer_id." and date='".date('Y-m-d')."'")->save(array('minus_num'=>($is_had['minus_num']+1)));
		}else{
			M('dealer_statistics')->add(array('dealer_id'=>$this->dealer_id,'date'=>date('Y-m-d'),'minus_num'=>1));
		}
		return M('dealer_repair_log')->add($data);
	}

	/**修改日志信息
	 * @param $id
	 * @param $data
	 * @return bool
	 */
	public function update_by_id($id,$data){
		return M('dealer_repair_log')->where('id='.$id)->save($data);
	}

	public function get_log_by_vin($vin){
		$map = array('vin'=>$vin,'dealer_id'=>$this->dealer_id,'search_state'=>1);
		return M('dealer_repair_log')->field('id')->where($map)->find();
	}

	/**
	 * 获取未支付信息
	 * @return mixed
	 */
	public function get_unpaid_log(){//'dealer_id='.$this->dealer_id.' and search_state = 2 and pay_state =0'
		$where = 'dealer_id='.$this->dealer_id.' and search_state = 2 and pay_state =0';
		$num = M('dealer_repair_log')->field('count(1) as times')->where($where)->find();
		$end =  M('dealer_repair_log')->field('date_format(createtime,"%Y-%m-%d") as createtime')->where($where)->order('createtime desc')->find();
		$first =M('dealer_repair_log')->field('date_format(createtime,"%Y-%m-%d") as createtime')->where($where)->order('createtime asc')->find();
		return array('times'=>$num['times'],'start_time'=>$first['createtime'],'end_time'=>$end['createtime']);
	}

	/**通过dealer_id 获取报告信息
	 * @return mixed
	 */
	public function get_log_by_dealer($vin){
		$map = array(
			'dealer_id'=>$this->dealer_id,
				'vin'=>$vin
		);
		return M('dealer_repair_log')->field('rid,dealer_id,search_state,vin,pay_state,createtime')
				->where($map)
				->order('createtime desc')
				->find();
	}

	/**获取正在查询中条数
	 * @return mixed
	 */
	public function get_dealer_search_credit(){
		 $nums = M('dealer_repair_log')->field('count(1) as nums')->where(array('dealer_id'=>$this->dealer_id,'search_state'=>1))->find();
		 return $nums['nums'];
	}

	/**
	 * @param $option
	 * @param $where
	 * @return bool
	 */
	public function update_by_vin($option,$where){
		 return M('dealer_repair_log')->where($where)->save($option);
	}


	public function get_not_pay_logs($where){
		 return M('dealer_repair_log')->where($where)->select();
	}

}
